{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# diet1_b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/diet1_b.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/diet1_b.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Simple diet problem in Google CP Solver.\n",
    "\n",
    "  Standard Operations Research example in Minizinc\n",
    "\n",
    "\n",
    "  Minimize the cost for the products:\n",
    "  Type of                        Calories   Chocolate    Sugar    Fat\n",
    "  Food                                      (ounces)     (ounces) (ounces)\n",
    "  Chocolate Cake (1 slice)       400           3            2      2\n",
    "  Chocolate ice cream (1 scoop)  200           2            2      4\n",
    "  Cola (1 bottle)                150           0            4      1\n",
    "  Pineapple cheesecake (1 piece) 500           0            4      5\n",
    "\n",
    "  Compare with the following models:\n",
    "  * Tailor/Essence': http://hakank.org/tailor/diet1.eprime\n",
    "  * MiniZinc: http://hakank.org/minizinc/diet1.mzn\n",
    "  * SICStus: http://hakank.org/sicstus/diet1.pl\n",
    "  * Zinc: http://hakank.org/minizinc/diet1.zinc\n",
    "  * Choco: http://hakank.org/choco/Diet.java\n",
    "  * Comet: http://hakank.org/comet/diet.co\n",
    "  * ECLiPSe: http://hakank.org/eclipse/diet.ecl\n",
    "  * Gecode: http://hakank.org/gecode/diet.cpp\n",
    "  * Gecode/R: http://hakank.org/gecode_r/diet.rb\n",
    "  * JaCoP: http://hakank.org/JaCoP/Diet.java\n",
    "\n",
    "  This version use ScalProd() instead of Sum().\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main(unused_argv):\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Diet\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 4\n",
    "  price = [50, 20, 30, 80]  # in cents\n",
    "  limits = [500, 6, 10, 8]  # requirements for each nutrition type\n",
    "\n",
    "  # nutritions for each product\n",
    "  calories = [400, 200, 150, 500]\n",
    "  chocolate = [3, 2, 0, 0]\n",
    "  sugar = [2, 2, 4, 4]\n",
    "  fat = [2, 4, 1, 5]\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  x = [solver.IntVar(0, 100, \"x%d\" % i) for i in range(n)]\n",
    "  cost = solver.IntVar(0, 10000, \"cost\")\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(solver.ScalProd(x, calories) >= limits[0])\n",
    "  solver.Add(solver.ScalProd(x, chocolate) >= limits[1])\n",
    "  solver.Add(solver.ScalProd(x, sugar) >= limits[2])\n",
    "  solver.Add(solver.ScalProd(x, fat) >= limits[3])\n",
    "\n",
    "  # objective\n",
    "  objective = solver.Minimize(cost, 1)\n",
    "\n",
    "  #\n",
    "  # solution\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.AddObjective(cost)\n",
    "  solution.Add(x)\n",
    "\n",
    "  # last solution since it's a minimization problem\n",
    "  collector = solver.LastSolutionCollector(solution)\n",
    "  search_log = solver.SearchLog(100, cost)\n",
    "  solver.Solve(\n",
    "      solver.Phase(x + [cost], solver.INT_VAR_SIMPLE, solver.ASSIGN_MIN_VALUE),\n",
    "      [objective, search_log, collector])\n",
    "\n",
    "  # get the first (and only) solution\n",
    "  print(\"cost:\", collector.ObjectiveValue(0))\n",
    "  print([(\"abcdefghij\" [i], collector.Value(0, x[i])) for i in range(n)])\n",
    "  print()\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "  print()\n",
    "\n",
    "\n",
    "main(\"cp sample\")\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
